import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lucky
 * Date: 2022-10-27
 * Time: 9:34
 */
class User{
    public int userId;
    public int classId;
}
@WebServlet("/postJson")
public class postjackson extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper(); //创建一个objectMapper对象
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取body中的请求，使用objectMapper解析成所需要的对象
        //readValue将JSON格式装换成字符转成Jackson格式
        //第一个参数表示随哪个字符串进行装换，可以使String、InputStrean、File，第二个参数表示把这个JSON格式的字符串装成哪个Java对象
        User user = objectMapper.readValue(req.getInputStream(),User.class);
       resp.getWriter().write("usertId=" + user.userId + "," + "classId=" +user.classId);
    }
}
